<template>
  <section style="text-align: left">
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      top="4%"
      @close="clear">
      <el-form :model="stockForm" ref="StockForm">
        <el-form-item label="供应商" label-width="120px" prop="supplier">
          <el-input v-model="stockForm.supplier" :disabled="disabled"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" label-width="120px" prop="name">
              <el-input v-model="stockForm.name"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进货总量" label-width="120px" prop="purchaseNum">
              <el-input v-model="stockForm.purchaseNum" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="进货总价" label-width="120px" prop="purchasePrice">
              <el-input v-model="stockForm.purchasePrice" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计划售价" label-width="120px" prop="sellPrice">
              <el-input v-model="stockForm.sellPrice"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预警数量" label-width="120px" prop="warn">
              <el-input v-model="stockForm.warn" placeholder="预警数量"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="描述" label-width="120px" prop="description">
          <el-input type="textarea" v-model="stockForm.description"/>
        </el-form-item>
        <el-form-item label-width="120px">
          <add-image ref="AddImage" @getDate="getDate"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onsubmit">确 定</el-button>
      </div>

    </el-dialog>
  </section>
</template>

<script>

import AddImage from "../../../other/component/AddImage";

export default {
  name: "StockAddOrEdit",
  components: {AddImage},
  data() {
    return {
      dialogFormVisible: false,
      title: '',
      disabled: false,
      stockForm: {
        stockId: '',
        supplier: '',
        name: '',
        purchaseNum: '',
        purchasePrice: '',
        sellPrice: '',
        description: '',
        warn: ''
      }

    }
  },
  methods: {
    edit(stock) {
      this.stockForm = stock
      this.disabled = true
    },

    onsubmit() {
      var data = Object.assign({}, this.stockForm)
      if (data.stockId === "") {
        data.warehousingDate = this.timeformate(new Date())
        data.createBy = this.$store.state.user.username
      }
      this.$axios.post("/content/stock/add_update", data).then(resp => {
        if (resp.data.code === 200) {
          this.$message({
            message: resp.data.result,
            type: "success"
          })
          this.dialogFormVisible = false
          this.clear()
          this.$emit("reload")
        }
      })
    },
    timeformate(date) {
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let hour = date.getHours();
      let minute = date.getMinutes();
      let second = date.getSeconds();
      return year + "-" + month + "-" + day + ' ' + hour + ":" + minute + ":" + second
    },
    clear() {
      this.stockForm = {
        stockId: '',
        supplier: '',
        name: '',
        purchaseNum: '',
        purchasePrice: '',
        sellPrice: '',
        description: '',
        warn: ''
      }
      this.disabled = false
    },
    getDate(){},
    AddImage() {
      this.$refs.AddImage.dialogFormVisible = true
    }
  }
}
</script>

<style scoped>

</style>
